package com.huawei.appgallery.festivalanimation.particle;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import com.huawei.appgallery.festivalanimation.particle.modifiers.IParticleModifier;

public class Particle {
    public float mAccelerationX;
    public float mAccelerationY;
    public int mAlpha;
    private int mBitmapHalfHeight;
    private int mBitmapHalfWidth;
    public float mCurrentX;
    public float mCurrentY;
    protected Bitmap mImage;
    public int mInitialAlpha;
    public float mInitialRotation;
    public float mInitialScale;
    private float mInitialX;
    private float mInitialY;
    private Matrix mMatrix;
    private Paint mPaint;
    private IParticleModifier mParticleModifier;
    private float mRotation;
    public float mRotationSpeed;
    public float mScale;
    public float mSpeedX;
    public float mSpeedY;
    protected long mStartingMilisecond;
    private long mTimeToLive;

    protected Particle() {
        this.mScale = 1.0f;
        this.mInitialScale = 1.0f;
        this.mAlpha = 255;
        this.mInitialAlpha = 255;
        this.mInitialRotation = 0.0f;
        this.mRotationSpeed = 0.0f;
        this.mSpeedX = 0.0f;
        this.mSpeedY = 0.0f;
        this.mMatrix = new Matrix();
        this.mPaint = new Paint(1);
    }

    protected Particle(boolean z) {
        this.mScale = 1.0f;
        this.mInitialScale = 1.0f;
        this.mAlpha = 255;
        this.mInitialAlpha = 255;
        this.mInitialRotation = 0.0f;
        this.mRotationSpeed = 0.0f;
        this.mSpeedX = 0.0f;
        this.mSpeedY = 0.0f;
        this.mMatrix = new Matrix();
        this.mPaint = new Paint(1);
        if (z) {
            this.mPaint.setXfermode(new PorterDuffXfermode(Mode.ADD));
        }
    }

    public Particle(Bitmap bitmap) {
        this();
        this.mImage = bitmap;
    }

    public Particle(Bitmap bitmap, boolean z) {
        this(z);
        this.mImage = bitmap;
    }

    public void init() {
        this.mScale = 1.0f;
        this.mAlpha = 255;
    }

    public void configure(long j, float f, float f2) {
        this.mBitmapHalfWidth = this.mImage.getWidth() / 2;
        this.mBitmapHalfHeight = this.mImage.getHeight() / 2;
        this.mInitialX = f - ((float) this.mBitmapHalfWidth);
        this.mInitialY = f2 - ((float) this.mBitmapHalfHeight);
        this.mCurrentX = this.mInitialX;
        this.mCurrentY = this.mInitialY;
        this.mTimeToLive = j;
    }

    public boolean update(long j) {
        long j2 = j - this.mStartingMilisecond;
        if (j2 > this.mTimeToLive) {
            return false;
        }
        this.mCurrentX = (this.mInitialX + (this.mSpeedX * ((float) j2))) + ((this.mAccelerationX * ((float) j2)) * ((float) j2));
        this.mCurrentY = (this.mInitialY + (this.mSpeedY * ((float) j2))) + ((this.mAccelerationY * ((float) j2)) * ((float) j2));
        this.mRotation = this.mInitialRotation + ((this.mRotationSpeed * ((float) j2)) / 1000.0f);
        this.mParticleModifier.modify(this, j2);
        return true;
    }

    public void draw(Canvas canvas) {
        this.mMatrix.reset();
        this.mMatrix.postRotate(this.mRotation);
        this.mMatrix.postScale(this.mScale, this.mScale, (float) this.mBitmapHalfWidth, (float) this.mBitmapHalfHeight);
        this.mMatrix.postTranslate(this.mCurrentX, this.mCurrentY);
        this.mPaint.setAlpha(this.mAlpha);
        canvas.drawBitmap(this.mImage, this.mMatrix, this.mPaint);
    }

    public Particle activate(long j, IParticleModifier iParticleModifier) {
        this.mStartingMilisecond = j;
        this.mParticleModifier = iParticleModifier;
        return this;
    }
}
